from pyecharts import options as opts
from pyecharts.charts import Radar
import numpy as np
import pandas as pd

class Draft_1year():
    def __init__(self, dataset, place=0, year='2015'):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1
        self.year=year

        #主程序入口
        self.main()
    
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘图函数
        self.paint()
        
        
    def getData(self):
            
        df = self.paint_data.loc[:,['date', 'flow','weather']] #提取日期、客流和天气数据
        self.paint_data=df.loc[df['date'].str.contains(self.year)] #筛选指定年份的数据
        # df['date'] = pd.to_datetime(df['date']) #将数据类型转换为日期类型
        # df = df.set_index('date') # 将date设置为index
        # self.paint_data=df[self.year] #筛选某一年的天气数据
        
        
    def paint(self):
        
        weather=np.array(self.paint_data)

        def all_np(arr):
            # arr = np.array(arr)
            key = np.unique(arr)
            result = {}
            for k in key:
                mask = (arr == k)
                arr_new = arr[mask]
                v = arr_new.size
                result[k] = v
            return result
        
        category=all_np(weather[:,-1]) #计算天气的种类
        
        # paint_data = {k: v for k, v in category.items() if v >= 35} #筛选出出现天数大于等于35天的天气
        paint_data=sorted(category.items(), key=lambda item:item[1],reverse = True);#将字典按values从高到低进行排序，生成的是list
        
        paint_data=dict(paint_data[0:7])# 将list转化为字典并选取前6种天气
        
        #将字典拆分成keys和values两部分
        keys = tuple(paint_data.keys())
        weathers=list(keys)
        # print(weathers)
        # values = tuple(self.paint_data.values())
        # num=list(values)
        means=[]
        medians=[]
        maxs=[]
        for weather in weathers:
            df=self.paint_data.loc[:,['flow','weather']]
            flow=df[df['weather']==weather]
            # print(flow)
            flow=np.array(flow['flow']) #将客流列转化为numpy数组

            mean=np.mean(flow) #求该天气下客流的均值
            means.append(int(mean)) #取整并存入list
            median=np.median(flow) #求该天气下客流的中位数
            medians.append(median) #存入list
            max=np.max(flow) #求该天气下客流的最大值
            maxs.append(max) #存入list      

        v1 = [means]
        v2 = [medians]
        
        if self.place==0:
            title='九寨沟'+self.year+'年不同天气下的客流情况'
        else:
            title='四姑娘山'+self.year+'年不同天气下的客流情况'
        
        fileName='3.5 '+title+'雷达图.html'
        
        c = (
            Radar()
            .add_schema(
                schema=[
                    opts.RadarIndicatorItem(name=weathers[0], max_=maxs[0]),
                    opts.RadarIndicatorItem(name=weathers[1], max_=maxs[1]),
                    opts.RadarIndicatorItem(name=weathers[2], max_=maxs[2]),
                    opts.RadarIndicatorItem(name=weathers[3], max_=maxs[3]),
                    opts.RadarIndicatorItem(name=weathers[4], max_=maxs[4]),
                    opts.RadarIndicatorItem(name=weathers[5], max_=maxs[5]),
                ]
            )
            .add("客流量均值", v1)
            .add("客流量中位数", v2)
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
            .set_global_opts(
                legend_opts=opts.LegendOpts(selected_mode="single"),
                title_opts=opts.TitleOpts(title=title),
            )
            .render('resultFile/'+fileName)
        )
        
        print(fileName+"已生成！")
